# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :jwt_tool.py
# @Time      :2022/6/29 9:35
# @Author    :吕华龙
import jwt
from flask import current_app
import time


# JWT的删除与验证模块
class JwtTool:
    # 生成token
    def create_token(self, payload, exp, secret=None):
        """
        :param payload:   载荷
        :param exp:     过期时间  秒数
        :param secret:   盐，可选
        :return:   JWT Token
        """
        if not secret:
            secret = current_app.config["SECRET_KEY"]

        payload["exp"] = int(time.time()) + exp  # 有效期 = 当前的时间 + 过期的时间

        token = jwt.encode(payload, key=secret, algorithm="HS256")

        return token

    # eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjMsInVzZXJuYW1lIjoiYWRtaW4iLCJleHAiOjE2NTczODAwMTJ9.Ph_K4lWWoiBixYs_ivC5DNMPhSFSiQjJbEl_lI5sGbc
    # eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjQsInVzZXJuYW1lIjoiYWRtaW5xd2UiLCJleHAiOjE2NTczODAzOTJ9.6 atuFAA7swDHxGCfyQDCdGKnwqVCHYH7XC25eFxUcAc
    # h：{jwt，hs256}
    # p:{uidusername}
    # 验证token
    def ver_token(self, token, secret=None):
        """
        验证token
        :param token:   要验证的Token
        :param secret:   盐，可选
        :return:   payload
        """
        if not secret:
            secret = current_app.config["SECRET_KEY"]
        try:
            payload = jwt.decode(token, secret, algorithms="HS256")
            # 判断token的有效期  有效期小于当前时间  说明过期了
            # if payload['exp'] < int(time.time()):
            #     print("过期了")
            #     payload = None
        except Exception as e:
            print("捕获异常了", e)
            payload = None
        return payload


if __name__ == '__main__':  # python  xxx.py 直接运行的时候才会执行，如果是导包导到别的地方，则不会执行
    from app import app

    with app.app_context():
        j = JwtTool()
        t = j.create_token({
            "uid": 100,
            "name": "张三"
        }, 5)
        print(t)
        t = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEwMCwibmFtZSI6Ilx1NWYyMFx1NGUwOSIsImV4cCI6MTY1NjQ3MTM3MX0.7WyWGejYexoaYs01xnngBDvm9cBVfPXhk8SXj-DapOQ"
        p = j.ver_token(t)
        print(t)
        print(p)

# ----------------------------------------------------------
# # 生成token
# class JwtTool:
#     # 创建token
#     def create_token(self, payload, exp, secret=None):
#         # 判断有没有盐
#         if not secret:
#             # 没有盐就用从对象中加载配置对象中的secret_key
#             secret = current_app.config["SECRET_KEY"]
#         # 过期时间等于  当前时间加上过期的时间等于有效期
#         payload['exp'] = int(time.time()) + exp
#         # 生成token加密
#         token = jwt.encode(payload, key=secret, algorithm="HS256")
#         return token
#
#     # 验证token
#     def ver_token(self, token, secret=None):
#         if not secret:
#             secret = current_app.config["SECRET_KEY"]
#         try:
#             payload = jwt.decode(token, secret, algorithms="HS256")
#             if payload['exp'] < int(time.time()):
#                 print("过期了")
#                 payload = None
#         except Exception as e:
#             print(e)
#             payload = None
#         return payload
